local jishan = fk.CreateSkill {
  name = "jishan",
}

jishan:addEffect(fk.DamageInflicted, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      return player:getMark("jishan_prevent-turn") == 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, { skill_name = self.name, prompt = "#jishan-invoke::" .. target.id })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "jishan_prevent-turn", 1)
    room:addTableMarkIfNeed(player, "jishan_record", target.id)
    room:loseHp(player, 1, self.name)
    if not player.dead then
      player:drawCards(1, self.name)
    end
    if not target.dead then
      target:drawCards(1, self.name)
    end
    return true
  end,
})
jishan:addEffect(fk.Damage, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      return target == player and player:getMark("jishan_recover-turn") == 0 and
          table.find(player.room.alive_players, function(to)
            return table.contains(player:getTableMark("jishan_record"), to.id) and to:isWounded() and
                table.every(player.room.alive_players, function(p) return p.hp >= to.hp end)
          end)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function(to)
      return table.contains(player:getTableMark("jishan_record"), to.id) and to:isWounded() and
          table.every(room.alive_players, function(p) return p.hp >= to.hp end)
    end)
    local to = room:askToChoosePlayers(player,
      {
        targets = targets,
        min_num = 1,
        max_num = 1,
        prompt = "#jishan-choose",
        skill_name = self.name,
        cancelable = true,
      })
    if #to > 0 then
      event:setCostData(self, { tos = to })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "jishan_recover-turn", 1)
    room:recover {
      who = event:getCostData(self).tos[1],
      num = 1,
      recoverBy = player,
      skillName = self.name
    }
  end,
})
return jishan
